//实现和声明放在一起
#include<iostream>
#include<string>
#include<unordered_map>
#include<fstream>
#include "inetAdder.hpp"
const std::string defaultpath = "./dictionary.txt";
const std::string sepchar = ":";
class Dictionary
{
    public:
    Dictionary(const std::string path = defaultpath)
    :_dict_path(path)
    {}
    bool loaddict()//这是打开文件的函数
    {
        std::ifstream in(_dict_path);
        if(!in.is_open())
        {
            std::cout<<"open file error"<<std::endl;
            return false;
        }
        //到这下面是能打开的
        //继续进行操作
        std::string line;
        while(std::getline(in,line))
        {
            //举个例子apple:苹果
            //第一步，先找到分隔符
            auto pos = line.find(sepchar);
            if(pos == std::string::npos)//找不到分隔符的情况
            {
                std::cout<<"find sepchar error"<<std::endl;
                continue;
            }
            std::string english = line.substr(0,pos);
            std::string chinese = line.substr(pos+sepchar.size());//不加后面，默认提取到结尾
            if(english.empty()||chinese.empty())
            {
                std::cout<<"no content error"<<std::endl;
            }
            _dict.insert(std::make_pair(english,chinese));
        }

        in.close();
        return true;
    }
    std::string Translate(const std::string &word,inetaddr &client)
    {
        std::cout<<"yes!!!!"<<std::endl;
        auto iter = _dict.find(word);
        if(iter == _dict.end())//找到最后也没有找到
        {
            return "Can not find the word";
        }
        std::cout<<"client ip:"<<client.Ip()<<","<<"client port:"<<client.Port()<<std::endl;
        return iter->second;
    }
    ~Dictionary()
    {}
    private:
    std::string _dict_path;
    std::unordered_map<std::string,std::string> _dict;
};